#!/usr/bin/env python

__AUTHOR__ = 'sykp241095@gmail.com'

import os
import re
import sys
import commands
from optparse import OptionParser

MDSTAT = '/proc/mdstat'

md_fl = re.compile('md\d+.*?[^\s[]+\[\d+\]', re.M)
md_sl = re.compile('\[[U_]*_[U_]*\]')

def check_raid():

    f = open(MDSTAT, 'r')
    while True:
        l = f.readline()
        if not l:
            break
        if md_fl.search(l.rstrip()):
            l = f.readline()
            if md_sl.search(l.strip()):
                l = f.readline()
                if l.find('recovery') < 0:
                    return 2
    return 0

def main():
    return check_raid()

if __name__ == '__main__':
    sys.exit(main())
